#!/bin/bash -e
yellow="\e[01;33m"
endcolor="\e[00m"

#check to see if root
if [[ "$(whoami)" != "root" ]]; then
	echo "This script must be run as root!"
	exit 0
fi

#greeting
clear
echo -e $yellow"Welcome to the Drupal automatic backup setup utility\n"
echo "This script will setup a new user on your system, that will perform the backup service for you, depending the schedule you specify."

#get backup user name
while [[ -z "$user" ]]; do
	echo -en $yellow"Please enter in the name for your backup user (required):"$endcolor
	read user
	
	if [[ "$(grep -c '^'$user':' /etc/passwd)"  = "1" ]]; then
		echo "This user exists. Please choose another user name"
		user=
	fi
done

#get backup user password
while [[ -z "$mysqlpswd" ]]; do
	echo -en $yellow"Please enter in the password for your backup user (required):"$endcolor
	read mysqlpswd
done


#get Mysql admin password
while [[ -z "$adminuser" ]]; do
	echo -en $yellow"Please enter MySQL admin user(required):"$endcolor
	read adminuser
done

#get MySQL admin password
while [[ -z "$adminmysqlpswd" ]]; do
	echo -en $yellow"Please enter MySQL admin user password (required):"$endcolor
	read adminmysqlpswd
done

#get database
while [[ -z "$db" ]]; do
	echo -en $yellow"Please enter the Drupal database name (required):"$endcolor
	read db
done

#get backup path
echo -en $yellow"Please enter your backup path hit enter for default path (/home/$user/backups):"$endcolor
read backuppath
if [[ -z $backuppath ]]; then
	backuppath="/home/$user/backups"
fi


while [[ -z $hour ]]; do
	echo -en $yellow"\nDo you want to run hourly backups (Y/N):"$endcolor
	read hour
done

while [[ -z $day ]]; do
	echo -en $yellow"\nDo you want to run daily backups (Y/N):"$endcolor
	read day
done

while [[ -z $week ]]; do
	echo -en $yellow"\nDo you want to run weekly backups (Y/N):"$endcolor
	read week
done
while [[ -z $month ]]; do
	echo -en $yellow"\nDo you want to run monthly backups (Y/N):"$endcolor
	read month
done

echo -e $yellow"Calculating file size requirements:"$endcolor
now=$(date +%s)

tables=$(mysql -D $db -u $adminuser --password=$adminmysqlpswd -B --column-names=False -e "SHOW TABLES where tables_in_$db not like 'cache_%'")

mysqldump $db -u $adminuser --password=$adminmysqlpswd $tables > /tmp/drupalbackuptest$now.sql

tar czfP /tmp/drupalbackuptest$now.sql.tgz /tmp/drupalbackuptest$now.sql

singlebackup=$(stat -c %s /tmp/drupalbackuptest$now.sql.tgz)


calculatedtotal=0



if [[ "$hour" = "Y" ]] ; then
	echo "0 * * * * drupalbackups -h" >> backupcron
	calculatedtotal=$[$calculatedtotal+($singlebackup*48)]
fi

if [[ "$day" = "Y" ]]; then
	echo "00 1 * * * drupalbackups -d" >> backupcron
	calculatedtotal=$[$calculatedtotal+($singlebackup*14)]
fi
if [[ "$week" = "Y" ]]; then
	echo "10 1 * * 0 drupalbackups -w" >> backupcron
	calculatedtotal=$[$calculatedtotal+($singlebackup*4)]
fi
if [[ "$month" = "Y" ]]; then
	echo "20 1 1 * * drupalbackups -m" >> backupcron
	calculatedtotal=$[$calculatedtotal+($singlebackup*12)]
fi

calculatedtotal=$[$calculatedtotal/1024]

echo -e $yellow"The estimated total backup size required is "$calculatedtotal"KB"$endcolor

find /tmp/ -type f -name "drupalbackuptest$now*" -exec rm {} \;

echo -en $yellow"Do you wish to continue?(Y/N)"$endcolor
read proceed
if [[ "$proceed" != "Y" || "proceed" = "y" ]]; then
	echo "Exiting, no changes saved"
	exit
fi

useradd -m $user
chown $user backupcron

echo "Writing user MySQL and drupalbackup configurations"
sudo -u $user echo -e "[client]\nuser = $user\npassword = $mysqlpswd\nhost = localhost\n\n[mysql]\ndatabase = $db\n" > /home/$user/.my.cnf
sudo -u $user echo -e "backup_path=$backuppath/" > /home/$user/.drupalbackup
echo "Creating backup directory"
sudo -u $user mkdir -p $backuppath




echo "Adding drupalbackup.sh to /usr/bin"

cp drupalbackups.sh /usr/bin/drupalbackups
chmod +x /usr/bin/drupalbackups
echo "Adding cron jobs."
echo $user >>/etc/cron.allow

sudo -u $user crontab backupcron
rm backupcron

echo "Creating MySQL user"

mysql mysql -u$adminuser --password=$adminmysqlpswd -e "GRANT SELECT, LOCK TABLES ON  $db.* TO  '$user'@'localhost' IDENTIFIED BY '$mysqlpswd';"








